package com.terrace.web.tagext.easyui;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.terrace.web.tagext.easyui.utils.EasyUIUtils;
import com.terrace.web.tagext.easyui.support.BaseTagSupport;

/**
 * 树
 * @author jiangyg
 *
 */
public class TreeTag extends BaseTagSupport {
	
	private static final long serialVersionUID = 7450104625678050960L;
	
	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(TreeTag.class);

	/**
	 * 标识
	 */
	private String name;
	
	/**
	 * panel标题
	 */
	private String title;
	
	/**
	 * 远程请求数据URL
	 */
	private String url;
	
	/**
	 * 提交事件
	 */
	private String submit;
	
	/**
	 * 是否面板包含
	 */
	private boolean isPanel = true;
	
	/**
	 * 动画效果
	 */
	private boolean animate = true;
	
	/**
	 * 是否显示复选框
	 */
	private boolean checkbox = true;
	
	/**
	 * 自动加载
	 */
	private boolean autoLoad = true;

	@Override
	public int doStartTag() throws JspException {
		return EVAL_PAGE;
	}

	@Override
	public int doEndTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			StringBuffer sb = new StringBuffer();
			if (isPanel) {
				sb.append("<div id=\"" + name + "-list-panel\" class=\"easyui-panel\" title=\"" + title + "\" ");
				sb.append("style=\"padding: 10px;\" data-options=\"fit:true,border:false,tools:'#" + name + "-list-panel-tools'\">");
			}
			sb.append("<ul id=\"" + name + "\" name=\"" + name + "\"></ul>");
			if (isPanel && checkbox) {
				sb.append("</div>");
				sb.append("<div id=\"" + name + "-list-panel-tools\">");
				sb.append("<a href=\"javascript:void(0)\" class=\"icon-ok\" onclick=\"" + name + "TreeSelectAll()\"></a>");
				sb.append("<a href=\"javascript:void(0)\" class=\"icon-standard-arrow-rotate-clockwise\" onclick=\"" + name + "TreeReset()\"></a>");
				if (StringUtils.isNotBlank(submit)) {
					sb.append("<a href=\"javascript:void(0)\" class=\"icon-save\" onclick=\"" + name + "TreeSubmit()\"></a>");
				}
				sb.append("</div>");
				
				sb.append("<script type=\"text/javascript\">");
				
				if (autoLoad && StringUtils.isNotBlank(url)) {
					sb.append("$('#" + name + "').tree({");
					EasyUIUtils.appendDatsOption(sb, "checkbox", checkbox);
					EasyUIUtils.appendDatsOption(sb, "url", url);
					sb.append("});");
				}
				
				// 提交函数
				if (StringUtils.isNotBlank(submit)) {
					sb.append("function " + name + "TreeSubmit(){" + submit + "(obtain" + name + "SelectDatas());}");
				}
				
				// 全部选择函数
				sb.append("function " + name + "TreeSelectAll() {var wtree = $('#" + name + "');var roots = wtree.tree('getRoots');");
				sb.append("for (var i = 0; i < roots.length; i++) {var node = wtree.tree('find', roots[i].id);wtree.tree('check', node.target);}}");
				
				// 重置函数
				sb.append("function " + name + "TreeReset() {$('#" + name + "').tree('reload');}");
				
				// 获取选中函数
				sb.append("function obtain" + name + "SelectDatas() {var ids = new Array();var wtree = $('#" + name + "');");
				sb.append("var roots = wtree.tree('getChecked');for (var i = 0; i < roots.length; i++) {");
				sb.append("if (wtree.tree('isLeaf', roots[i].target)) {var parentRoots = wtree.tree('getParent', roots[i].target);");
				sb.append("while (parentRoots != null) {ids.push(parentRoots.id);parentRoots = wtree.tree('getParent', parentRoots.target);");
				sb.append("}ids.push(roots[i].id);}}$.unique(ids);return ids.join(',');};");
				
				sb.append("</script>");
			}
			
			out.print(sb.toString());
		} catch (Exception e) {
			logger.error("生成tree树失败：", e);
		}
		return EVAL_PAGE;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getSubmit() {
		return submit;
	}

	public void setSubmit(String submit) {
		this.submit = submit;
	}

	public boolean isPanel() {
		return isPanel;
	}

	public void setPanel(boolean isPanel) {
		this.isPanel = isPanel;
	}

	public boolean isAnimate() {
		return animate;
	}

	public void setAnimate(boolean animate) {
		this.animate = animate;
	}

	public boolean isCheckbox() {
		return checkbox;
	}

	public void setCheckbox(boolean checkbox) {
		this.checkbox = checkbox;
	}

	public boolean isAutoLoad() {
		return autoLoad;
	}

	public void setAutoLoad(boolean autoLoad) {
		this.autoLoad = autoLoad;
	}

}
